package systempen.cmanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DialogTypeBrowser extends Dialog implements OnClickListener {
	Button okButton;
	Button cancelButton;
	ListView lw;
	String level;
	IFoundCostType ifo;
	public OnItemClickListener kl;
	public DialogTypeBrowser(Context context, String level, IFoundCostType iFoundCos) {
		super(context);
		ifo = iFoundCos;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_type_browser);
		okButton = (Button) findViewById(R.id.dialog_type_browser_ok);
		okButton.setOnClickListener(this);
		cancelButton = (Button) findViewById(R.id.dialog_type_browser_cancel);
		cancelButton.setOnClickListener(this);
		lw = (ListView) findViewById(R.id.dialog_type_browser_list);
		this.level = level;
		lw.setOnItemClickListener(kl);
		fillList(level);

	}
	@Override
	public void onClick(View v) {
		/** When OK Button is clicked, dismiss the dialog */
		if (v == cancelButton)
			dismiss();
		if (v==okButton){
			if (level.length() ==0){			
				Toast.makeText(getContext(),"Level can't be empty.", Toast.LENGTH_LONG).show();
				dismiss();
			}
			onFoundCostType(level);
			dismiss();
		}
		else{
			//
			dismiss();
			onFoundCostType("0");
		}		
	}
	public void fillList(final String Lev){
		Cursor c;
		ArrayList<ListItemDialogTypeBrowser> m_Devices = new ArrayList<ListItemDialogTypeBrowser>();
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
		util u = new util();
		c = u.getCursor("select name as name, code as _id, code as code from cost_type where parent ="+Lev.toString(), getContext());
		c.moveToFirst();
		//int i = c.getCount();
		if (c.getCount()>0){
			if (c.moveToFirst()) { 
				//ArrayList<String> strings = new ArrayList<String>();
				ListItemDialogTypeBrowser device;
				do {
					//String mC = c.getString(0);
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("name",c.getString(0));
					String numberOfSubrtpes = util.getSubtypes(c.getString(2).toString(),getContext());
					if (numberOfSubrtpes  == null)
						numberOfSubrtpes = "0";
					map.put("code",c.getString(2));
					fillMaps.add(map);
					//strings.add(mC); //cemu ovo sluzi?
					String code  = c.getString(2);
					String opis = c.getString(0);
					device = new ListItemDialogTypeBrowser(code,opis);
					m_Devices.add(device);
				}while (c.moveToNext()); 
			} 
		}
		c.close();
		u.db.close();
		ListAdapterDialogTypeBrowser lvAdapter =  new ListAdapterDialogTypeBrowser(getContext(), R.layout.item_dialog_type_browser, m_Devices);
		//setListAdapter(lvAdapter);
		lw.setAdapter(lvAdapter);//valjda je ovo?
		kl = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				View name = ((LinearLayout) arg1).getChildAt(0);
				String sName = ((TextView)name).getText().toString();
				//Toast.makeText(getContext(),"Costtypes: FFS: "+sName, Toast.LENGTH_LONG).show();
				if (util.existsChildCost(sName, getContext())){
					level = sName; 
					lw.setAdapter(null);
					fillList(level);
				}else{
					//Toast.makeText(getContext(),"Zis one has not subtypes "+sName, Toast.LENGTH_SHORT).show();
					level = sName;
					onFoundCostType(level);
					dismiss();
				}
			}

		};
		lw.setOnItemClickListener(kl);
	}
	public interface IFoundCostType{
		public void onFoundCostType(String costType);
	}
	public void onFoundCostType(String costType){
		if (ifo!=null){
			ifo.onFoundCostType(costType);
		}

	}

}